package product;

import models.BaseModel;
import models.constants.DeletedStatus;
import play.modules.paginate.JPAExtPaginator;

import javax.persistence.*;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/22.
 */
@Entity
@Table(name="area")
public class Area extends BaseModel{

    @Column(name="name")
    public String name;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;
    /**
     * 分页查询.
     */
    public static JPAExtPaginator<Area> findByCondition(Map<String, Object> conditionMap, String orderByExpress, int pageNumber, int pageSize) {
        StringBuilder xsqlBuilder = new StringBuilder("t.deleted=models.constants.DeletedStatus.UN_DELETED ")
                .append("/~ and t.name like {name} ~/");

        util.xsql.XsqlBuilder.XsqlFilterResult result = new util.xsql.XsqlBuilder().generateHql(xsqlBuilder.toString(), conditionMap);
        JPAExtPaginator<Area> orderPage = new JPAExtPaginator<Area>("Area t", "t", Area.class,
                result.getXsql(), conditionMap).orderBy(orderByExpress);
        orderPage.setPageNumber(pageNumber);
        orderPage.setPageSize(pageSize);
        orderPage.setBoundaryControlsEnabled(false);
        return orderPage;
    }
    public static Area fingById(Long id){
        return  Area.find("deleted = 0 and id= ?",id).first();
    }

    public static List<Area> findByDeleted (DeletedStatus deletedStatus) {
        return  Area.find("deleted = ? ",deletedStatus).fetch();
    }
}

